package views;

import views.panels.PanelGeneral;
import logicBusiness.AreaBL;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import logicBusiness.AsignaturaBL;
import models.Asignatura;
import javax.swing.JOptionPane;

/**
 *
 * @author viviana
 */
public class InterfazEditarAsignatura extends javax.swing.JFrame {

    AreaBL areaBo = new AreaBL();
    AsignaturaBL asignaturaBo = new AsignaturaBL();
    Integer ok;

    /** Creates new form InterfazEditarAsignatura */
    public InterfazEditarAsignatura() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);
        cargarListaGrados();
        cargarListaAreas();
        cargarListaIntensidadHoraria();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        CBGrado = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        CBArea = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        CBNomAsignatura = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        CTNombreAsignatura = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        CBGradoNuevo = new javax.swing.JComboBox();
        CBAreaNueva = new javax.swing.JComboBox();
        CBIntensidadHoraria = new javax.swing.JComboBox();
        BModificar = new javax.swing.JButton();
        BEliminar = new javax.swing.JButton();
        BAceptar = new javax.swing.JButton();

        setResizable(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 240));
        jLabel2.setText("BUSCAR ASIGNATURA");

        jLabel3.setText("Grado al que se dicta:");

        CBGrado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        CBGrado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBGradoActionPerformed(evt);
            }
        });

        jLabel4.setText("Area:");

        CBArea.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        CBArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBAreaActionPerformed(evt);
            }
        });

        jLabel5.setText("Nombre de la Asignatura:");

        CBNomAsignatura.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        CBNomAsignatura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBNomAsignaturaActionPerformed(evt);
            }
        });

        jLabel7.setText("Nuevo nombre de la asignatura:");

        CTNombreAsignatura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CTNombreAsignaturaActionPerformed(evt);
            }
        });

        jLabel8.setText("Grado al que se dicta:");

        jLabel9.setText("Area:");

        jLabel10.setText("Intensidad Horaria (en horas a la semana)");

        CBGradoNuevo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        CBGradoNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBGradoNuevoActionPerformed(evt);
            }
        });

        CBAreaNueva.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        CBIntensidadHoraria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        BModificar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BModificar.setText("Modificar");
        BModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BModificarActionPerformed(evt);
            }
        });

        BEliminar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BEliminar.setText("Eliminar");
        BEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BEliminarActionPerformed(evt);
            }
        });

        BAceptar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BAceptar.setText("Aceptar");
        BAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(CBGrado, 0, 581, Short.MAX_VALUE)
                    .addComponent(CBArea, 0, 581, Short.MAX_VALUE)
                    .addComponent(CBNomAsignatura, javax.swing.GroupLayout.Alignment.TRAILING, 0, 581, Short.MAX_VALUE)
                    .addComponent(CBIntensidadHoraria, 0, 581, Short.MAX_VALUE)
                    .addComponent(CBAreaNueva, 0, 581, Short.MAX_VALUE)
                    .addComponent(CBGradoNuevo, 0, 581, Short.MAX_VALUE)
                    .addComponent(CTNombreAsignatura, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 581, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(340, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(345, 345, 345))
            .addGroup(layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addComponent(BModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 333, Short.MAX_VALUE)
                .addComponent(BEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(73, 73, 73))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(328, Short.MAX_VALUE)
                .addComponent(BAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(288, 288, 288))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(CBGrado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CBArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBNomAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CTNombreAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBGradoNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBAreaNueva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(CBIntensidadHoraria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BModificar)
                    .addComponent(BEliminar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BAceptar)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BModificarActionPerformed

        ok = 0;
        Asignatura tst;

        if (CTNombreAsignatura.getText().equals("") || CBIntensidadHoraria.getSelectedIndex() == 0 || CBGradoNuevo.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Debe digitar un nombre, seleccionar una Intensidad Horaria, escoger un grado y un area", null, JOptionPane.ERROR_MESSAGE);
        } else {
            tst = asignaturaBo.consultarAsignatura(CTNombreAsignatura.getText(), CBGradoNuevo.getSelectedIndex() - 1);
//            if (tst == null || (tst.getNombreAsignatura().equals(CTNombreAsignatura.getText()) && tst.getIdGrado().equals(CBGradoNuevo.getSelectedIndex() - 1))) {
//                ok = asignaturaBo.actualizarAsignatura((CBNomAsignatura.getSelectedItem() + ""), CBGrado.getSelectedIndex() - 1, CTNombreAsignatura.getText(), CBIntensidadHoraria.getSelectedIndex(), CBGradoNuevo.getSelectedIndex() - 1, (CBAreaNueva.getSelectedItem() + ""));
//                CTNombreAsignatura.setText("");
//                cargarListaGrados();
//                cargarListaAreas();
//                cargarListaIntensidadHoraria();
//            }
            if (ok == 1) {
                JOptionPane.showMessageDialog(this, "La modificación de la asignatura fue exitosa", "Modificación exitosa !!!", JOptionPane.INFORMATION_MESSAGE);
            } else {
                if (tst != null) {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al modificar la asignatura (Asignaturas Repetidas)", "Modificación fallida", JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al modificar la asignatura", "Modificación fallida", JOptionPane.ERROR_MESSAGE);
                }
            }

        }







    }//GEN-LAST:event_BModificarActionPerformed

    private void BEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BEliminarActionPerformed
        if (CBNomAsignatura.getSelectedIndex() == -1 || CBGrado.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "No hay asignaturas que borrar", "Eliminación fallida", JOptionPane.ERROR_MESSAGE);
        } else {
            ok = asignaturaBo.encontrarAsignatura(CBNomAsignatura.getSelectedItem() + "", CBGrado.getSelectedIndex() - 1);
            if (ok != 0) {
                asignaturaBo.borrarAsignatura(ok);
                JOptionPane.showMessageDialog(this, "Eliminado correctamente", "Eliminación correcta", JOptionPane.OK_OPTION);
                cargarListaGrados();
                cargarListaAreas();
                cargarListaIntensidadHoraria();
            }
        }


    }//GEN-LAST:event_BEliminarActionPerformed

    private void CBAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBAreaActionPerformed
        cargarListaAsignaturas();
    }//GEN-LAST:event_CBAreaActionPerformed

    private void CBNomAsignaturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBNomAsignaturaActionPerformed
        // TODO add your handling code here:
        if (CBNomAsignatura.getSelectedItem() != null) {
            CTNombreAsignatura.setText(CBNomAsignatura.getSelectedItem() + "");
        } else {
            CTNombreAsignatura.setText("");
        }
        CBGradoNuevo.setSelectedIndex(CBGrado.getSelectedIndex());
        CBAreaNueva.setSelectedIndex(CBArea.getSelectedIndex());
    }//GEN-LAST:event_CBNomAsignaturaActionPerformed

    private void CTNombreAsignaturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CTNombreAsignaturaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CTNombreAsignaturaActionPerformed

    private void CBGradoNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBGradoNuevoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CBGradoNuevoActionPerformed

    private void BAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BAceptarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_BAceptarActionPerformed

    private void CBGradoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBGradoActionPerformed
        cargarListaAsignaturas();
    }//GEN-LAST:event_CBGradoActionPerformed

    private void cargarListaGrados() {
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) CBGrado.getModel();
        DefaultComboBoxModel modelo1 = (DefaultComboBoxModel) CBGradoNuevo.getModel();
        modelo.removeAllElements();
        modelo1.removeAllElements();
        modelo.addElement("Seleccione");
        modelo.addElement("0.  Transicion");
        modelo.addElement("1.  Primero");
        modelo.addElement("2.  Segundo");
        modelo.addElement("3.  Tercero");
        modelo.addElement("4.  Cuarto");
        modelo.addElement("5.  Quinto");
        modelo.addElement("6.  Sexto");
        modelo.addElement("7.  Séptimo");
        modelo.addElement("8.  Octavo");
        modelo.addElement("9.  Noveno");
        modelo.addElement("10.  Decimo");
        modelo.addElement("11.  Once");

        modelo1.addElement("Seleccione");
        modelo1.addElement("0.  Transicion");
        modelo1.addElement("1.  Primero");
        modelo1.addElement("2.  Segundo");
        modelo1.addElement("3.  Tercero");
        modelo1.addElement("4.  Cuarto");
        modelo1.addElement("5.  Quinto");
        modelo1.addElement("6.  Sexto");
        modelo1.addElement("7.  Séptimo");
        modelo1.addElement("8.  Octavo");
        modelo1.addElement("9.  Noveno");
        modelo1.addElement("10.  Decimo");
        modelo1.addElement("11.  Once");

    }

    private void cargarListaAreas() {
        List l = areaBo.consultarAreas();
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) CBArea.getModel();
        DefaultComboBoxModel modelo1 = (DefaultComboBoxModel) CBAreaNueva.getModel();
        modelo.removeAllElements();
        modelo1.removeAllElements();
        for (int i = 0; i < l.size(); i++) {
            modelo.addElement(l.get(i));
            modelo1.addElement(l.get(i));

        }
        CBArea.setModel(modelo);
    }

    private void cargarListaAsignaturas() {
        List a = asignaturaBo.consultarAsignaturasAreaGrado(CBArea.getSelectedItem() + "", CBGrado.getSelectedIndex() - 1);

        DefaultComboBoxModel modelo = (DefaultComboBoxModel) CBNomAsignatura.getModel();
        modelo.removeAllElements();
        for (int i = 0; i < a.size(); i++) {
            modelo.addElement(a.get(i));

        }
        CBNomAsignatura.setModel(modelo);


    }

    private void cargarListaIntensidadHoraria() {
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) CBIntensidadHoraria.getModel();
        modelo.removeAllElements();
        modelo.addElement("Seleccione");
        modelo.addElement("1 Hora");
        modelo.addElement("2 Horas");
        modelo.addElement("3 Horas");
        modelo.addElement("4 Horas");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazEditarAsignatura().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BAceptar;
    private javax.swing.JButton BEliminar;
    private javax.swing.JButton BModificar;
    private javax.swing.JComboBox CBArea;
    private javax.swing.JComboBox CBAreaNueva;
    private javax.swing.JComboBox CBGrado;
    private javax.swing.JComboBox CBGradoNuevo;
    private javax.swing.JComboBox CBIntensidadHoraria;
    private javax.swing.JComboBox CBNomAsignatura;
    private javax.swing.JTextField CTNombreAsignatura;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    // End of variables declaration//GEN-END:variables
}
